import 'package:flutter_crud_test_amir_rabbani/core/repository/base_local_repository.dart';
import 'package:flutter_crud_test_amir_rabbani/data/local/my_database.dart';

abstract class CustomerDataSource {
  Future<void> create(CustomerDaoCompanion params);

  Future edit(int id, CustomerDaoCompanion params);

  Future delete(int params);

  Stream read();

  Future readById(int id);
}

class CustomerDataSourceImpl extends BaseLocalRepository
    implements CustomerDataSource {
  CustomerDataSourceImpl(super.database);

  @override
  Future<void> create(CustomerDaoCompanion params) async {
    database.into(database.customerDao).insert(params);
  }

  @override
  Future<void> delete(int id) async {
    final query = database.delete(database.customerDao)
      ..where((tbl) => tbl.id.equals(id));
    query.go();
  }

  @override
  Future<void> edit(int id, CustomerDaoCompanion params) async {
    database.update(database.customerDao)
      ..where((tbl) => tbl.id.equals(id))
      ..write(params);
  }

  @override
  Stream read() {
    final query = database.select(database.customerDao);
    return query.watch();
  }

  @override
  Future readById(int id) {
    final query = database.select(database.customerDao)
      ..where((tbl) => tbl.id.equals(id));
    return query.get();
  }
}
